package com.example.springbootwebsocketmonikehuduan.kdgctest;

import com.example.springbootwebsocketmonikehuduan.WebsocketClient333;
import com.example.springbootwebsocketmonikehuduan.entity.GatherMjModel;
import com.example.springbootwebsocketmonikehuduan.entity.SignalMjDhModel;
import com.example.springbootwebsocketmonikehuduan.utils.SpringContextUtil;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * 文件描述
 *
 * @author xianjin.lu
 * @date 2021
 */
public class MjProcess {

    private static Logger logger = Logger.getLogger(MjProcess.class);
    //param  是  gatherId
    private String gatherId;

    public MjProcess(String param) {
        this.gatherId = param;
    }

    public void collectProcessor() throws Exception {

        MjWebsocketClient websocketClient = new MjWebsocketClient();

        //获取倒数第二个字符
        char two = gatherId.charAt(gatherId.length() - 2);
        int twoint=two-48;
        System.out.println(twoint);
        //获取倒数第一个个字符
        char one = gatherId.charAt(gatherId.length() - 1);
        int oneint=one-48;
        System.out.println(oneint);

        if(twoint==0){

            String  s=   "{'head':{'cmdType':2,'cmdSn':'0002'},'data':{'devId':"+oneint+",'operType':0,'doorIndex':0}}";
            System.out.println(s);
            websocketClient.find(s);
        }else{
            int total=twoint*10+oneint;
            String  s=   "{'head':{'cmdType':2,'cmdSn':'0002'},'data':{'devId':"+total+",'operType':0,'doorIndex':0}}";
            websocketClient.find(s);
        }


        int time=0;
        while(websocketClient.getMap().isEmpty()){
            System.out.println("正在等待获取消息");
            Thread.sleep(300);
            time++;
            if (time==20){
                break;   //while循环终止     return结束collectProcessor()方法
            }
        }

        Map<String, Object> map=websocketClient.getMap();

        //关闭websocket连接
        websocketClient.client.close();
        if (map.isEmpty()){
            logger.info("websocket消息获取超时.请联系管理员");
        }
        {

            SignalMjDhModel automaticControlModel = new SignalMjDhModel();
            for (String s : map.keySet()) {

                String openState = map.get(s).toString();
                automaticControlModel.setGatherId(gatherId);
                automaticControlModel.setGatherResId(gatherId + "0001");
                automaticControlModel.setSignalType("MJ");//设备状态信号
                automaticControlModel.setPointAddr(s);   //设置pointAddr  对应设备id
                automaticControlModel.setSignalValue(openState);  //设置信号值
                switch (s) {    //设置设备编码
                    case "1":
                        automaticControlModel.setDevCode("XF-1427-J001-06-0271");
                        break;
                    case "2":
                        automaticControlModel.setDevCode("XF-1427-J001-07-0271");
                        break;
                    case "3":
                        automaticControlModel.setDevCode("XF-1427-C001-08-0271");
                        break;
                    case "4":
                        automaticControlModel.setDevCode("XF-1427-C001-08-0272");
                        break;
                    case "5":
                        automaticControlModel.setDevCode("XF-1427-C002-08-0271");
                        break;
                    case "6":
                        automaticControlModel.setDevCode("XF-1427-C002-07-0272");
                        break;
                    case "7":
                        automaticControlModel.setDevCode("XF-1427-C002-07-0273");
                        break;
                    case "8":
                        automaticControlModel.setDevCode("XF-1427-J002-06-0271");
                        break;
                    case "9":
                        automaticControlModel.setDevCode("XF-1427-J002-07-0271");
                        break;
                    case "11":
                        automaticControlModel.setDevCode("XF-1427-J003-06-0271");
                        break;
                    case "12":
                        automaticControlModel.setDevCode("XF-1427-J003-07-0271");
                        break;
                    case "15":
                        automaticControlModel.setDevCode("XF-1497-J001-06-0271");
                        break;
                    case "16":
                        automaticControlModel.setDevCode("XF-1497-J001-07-0271");
                        break;
                    case "17":
                        automaticControlModel.setDevCode("XF-1497-J002-06-0271");
                        break;
                    case "18":
                        automaticControlModel.setDevCode("XF-1497-J002-07-0271");
                        break;
                    default:
                        System.out.println("没有找到对应设备");
                }
                System.out.println(automaticControlModel);
            }

            logger.info("............数据解析结束................");

        }
    }
}